package com.shycloud.mido.music.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.music.domain.MisRedeemMusicCollection;
import com.shycloud.mido.music.domain.MisUserBuyCollection;
import com.shycloud.mido.music.mapper.MisUserBuyCollectionMapper;
import com.shycloud.mido.music.service.MisRedeemMusicCollectionService;
import com.shycloud.mido.music.service.impl.MisUserBuyCollectionService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 兑换曲谱 controller
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/5/26
 */
@RestController
@RequestMapping("redeem")
@AllArgsConstructor
@ApiOperation("兑换曲谱")
@ApiIgnore
public class RedeemMusicCollectionController {

	/** 兑换曲谱 service */
	private MisRedeemMusicCollectionService misRedeemMusicCollectionService;
	/** 上传图片 template */
	private MinioTemplate minioTemplate;
	/** 用户开通单本权限模块 service */
	private MisUserBuyCollectionService buyCollectionService;

	private MisUserBuyCollectionMapper buyCollectionMapper;

	/**
	 * 分页获取兑换曲谱信息
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/26 19:21
	 */
	@GetMapping("getRedeem")
	@ApiOperation("分页获取兑换曲谱信息")
	public R getRedeemMusicCollection(Integer current, Integer size, String phone, String bookNumber) {

		//分页参数
		Page page = new Page(current, size);
		//分页获取【兑换曲谱】信息
		return new R(misRedeemMusicCollectionService.getRedeemMusicCollection(page, phone, bookNumber));

	}

	/**
	 * 上传条码图片
	 *
	 * @param img 图片URL
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/30 9:26
	 */
	@PostMapping("uploadImage")
	@ApiOperation(value = "上传条码图片")
	public R uploadImage(@RequestParam("img") MultipartFile img) {

		//提取后缀
		//提取图片名称
		String fileName = img.getOriginalFilename().replace(" ", "");
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

		String uuid = UUID.randomUUID().toString();
		//提取图片名称
		String imgName = uuid.replace("-", "") + "." + suffix;
		//初始化map结果集
		Map<String, String> resultMap = new HashMap<>(4);
		//名称
		resultMap.put("name", imgName);
		try {
			//上传操作
			resultMap.put("url", minioTemplate.uploadFile(img.getInputStream(), imgName));
		} catch (Exception e) {
			return R.builder().code(CommonConstants.FAIL).msg(e.getLocalizedMessage()).build();
		}
		return R.builder().data(resultMap).build();

	}

	/**
	 * 兑换曲谱
	 *
	 * @param redeem
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/27 14:14
	 */
	@PostMapping("addRedeem")
	@ApiOperation("兑换曲谱")
	public R insertRedeem(@RequestBody MisRedeemMusicCollection redeem) {

		if (redeem.getMemberId().isEmpty()) {
			return new R(CommonConstants.FAIL, "请点击查询按钮", false);
		}
		if (redeem.getBookNumber() == null) {
			return new R(CommonConstants.FAIL, "请输入曲谱编号", false);
		}
		if (redeem.getCollectionId() == null) {
			return new R(CommonConstants.FAIL, "请选择开通的曲谱", false);
		}

		//校验是否已经购买了该曲集
		Integer count = buyCollectionMapper.checkIsByCollection(redeem.getMemberId(), Integer.parseInt(redeem.getCollectionId()));

		if (count > 0) {
			throw new BusinessException("此用户已经激活了本书，无需重复激活");
		}

		MisUserBuyCollection userBuyCollection = new MisUserBuyCollection();
		//用户ID
		userBuyCollection.setMemberId(redeem.getMemberId());
		//曲集ID
		userBuyCollection.setCollectionId(Integer.parseInt(redeem.getCollectionId()));
		//开通日期
		userBuyCollection.setCreateTime(new Date());
		//类型
		userBuyCollection.setType("4");
		//TODO 后台管理兑换的曲谱不显示在APP首页
		userBuyCollection.setIsShow("0");

		//开通单本权限
		buyCollectionService.save(userBuyCollection);
		return new R(misRedeemMusicCollectionService.save(redeem));
	}

	/**
	 * 获取有声图书数量
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/28 9:42
	 */
	@GetMapping("getBookCount")
	@ApiOperation("获取有声图书数量")
	public R getBookCount() {
		return new R(buyCollectionService.count());
	}

}
